﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.DXErrorProvider;

namespace Loki.UI.Win
{
    public partial class ErrorSummary : XtraForm
    {
        public ErrorSummary()
        {
            InitializeComponent();
        }

        public static void DisplayErrorSummary(params DXValidationProvider[] P_Validators)
        {
            Dictionary<string, IEnumerable<DXValidationProvider>> L_Vaidators = new Dictionary<string, IEnumerable<DXValidationProvider>>();
            L_Vaidators[String.Empty] = P_Validators;
            DisplayErrorSummary(L_Vaidators);
        }

        public static void DisplayErrorSummary(Dictionary<string, IEnumerable<DXValidationProvider>> P_Validators)
        {
            Dictionary<string, IEnumerable<ValidationMessage>> L_Messages = new Dictionary<string, IEnumerable<ValidationMessage>>();
            foreach (string L_Prefix in P_Validators.Keys)
            {
                List<ValidationMessage> L_MessageList = new List<ValidationMessage>();
                foreach (DXValidationProvider L_Provider in P_Validators[L_Prefix])
                {
                    foreach (Control L_Control in L_Provider.GetInvalidControls())
                    {
                        ValidationRuleBase L_Rule = L_Provider.GetValidationRule(L_Control);
                        L_MessageList.Add(new ValidationMessage() { Message = L_Rule.ErrorText, Type = L_Rule.ErrorType });
                    }
                }
                L_Messages[L_Prefix] = L_MessageList;
            }
            DisplayErrorSummary(L_Messages);
        }

        public static void DisplayErrorSummary(params ValidationMessage[] P_Validators)
        {
            Dictionary<string, IEnumerable<ValidationMessage>> L_Vaidators = new Dictionary<string, IEnumerable<ValidationMessage>>();
            L_Vaidators[String.Empty] = P_Validators;
            DisplayErrorSummary(L_Vaidators);
        }

        public static void DisplayErrorSummary(Dictionary<string, IEnumerable<ValidationMessage>> P_Messages)
        {
            ErrorSummary L_MessageBox = new ErrorSummary();
            try
            {
                foreach (string L_Prefix in P_Messages.Keys)
                {
                    foreach (ValidationMessage L_Message in P_Messages[L_Prefix])
                    {
                        L_MessageBox.LST_Errors.Items.Add(L_Prefix + L_Message.Message, L_MessageBox.GetIconIndex(L_Message.Type));
                    }
                }
                L_MessageBox.ShowDialog();
            }
            finally
            {
                L_MessageBox.Dispose();
            }
        }

        private int GetIconIndex(ErrorType P_Type)
        {
            switch (P_Type)
            {
                case ErrorType.Critical:
                case ErrorType.Default:
                    return 1;
                case ErrorType.Information:
                    return 0;
                case ErrorType.Warning:
                    return 2;
                case ErrorType.None:
                case ErrorType.User1:
                case ErrorType.User2:
                case ErrorType.User3:
                case ErrorType.User4:
                case ErrorType.User5:
                case ErrorType.User6:
                case ErrorType.User7:
                case ErrorType.User8:
                case ErrorType.User9:
                default:
                    return -1;
            }
        }
    }
}
